package com.jh3502.helper.function.batch.batchnew;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jh3502.helper.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author harisucici
 */
public class BatchNewAdapter extends RecyclerView.Adapter<BatchNewAdapter.MyViewHolder> {



    private List<BatchNewBean> data;
    private Context context;
    private int[] mustContain = null;

    private OnRecyclerViewItemClickListener mOnItemClickListener;
    public BatchNewAdapter(Context context, List<BatchNewBean> data) {
        this.context = context;
        this.data = data;

    }
    public BatchNewAdapter(Context context, List<BatchNewBean> data, int[] mustContain) {
        this.context = context;
        this.data = data;
        this.mustContain = mustContain;

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_batch_new, parent, false));
        return holder;
    }

    // 判断必填项 loop 效率最高
    private boolean useLoop(int[] arr,int value){
        for(int i:arr){
            if(i==value) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        final BatchNewBean label = data.get(position);
        holder.dispatchNameItem.setText(label.getName());
        holder.dispatchRadiobuttonItem.setId(position);
        if (mustContain!=null){// 非特体
            if(useLoop(mustContain,position)){// 必填项
                holder.dispatchRadiobuttonItem.setChecked(true);
            }else{
                if (label.getIsCheck()) {
                    holder.dispatchRadiobuttonItem.setChecked(true);
                } else {
                    holder.dispatchRadiobuttonItem.setChecked(false);
                }
            }
        }else{
            if (label.getIsCheck()) {
                holder.dispatchRadiobuttonItem.setChecked(true);
            } else {
                holder.dispatchRadiobuttonItem.setChecked(false);
            }
        }



        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(holder.itemView, position, label.getIsCheck());
            }
        };
        holder.itemView.setOnClickListener(clickListener);
        holder.dispatchRadiobuttonItem.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public interface OnRecyclerViewItemClickListener {

        void onItemClick(View view, int position, boolean isCheck);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.dispatch_radiobutton_item)
        RadioButton dispatchRadiobuttonItem;
        @BindView(R.id.dispatch_name_item)
        TextView dispatchNameItem;

        public MyViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}

